﻿using D3StatsClient.Model;
using D3StatsClient.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace D3StatsClient.Commands
{
    public class LoadSession : CommandBase
    {
        public override bool CanExecute(object parameter)
        {
            var vm = ViewModelManager<SessionPickerWindowViewModel>.ViewModel;

            if (vm.SelectedSession != null)
            {
                return true;
            }

            return false;
        }

        public override void Execute(object parameter)
        {
            var mwvm = ViewModelManager<MainWindowViewModel>.ViewModel;
            var vm = ViewModelManager<SessionPickerWindowViewModel>.ViewModel;

            mwvm.provider = new XMLStatsProvider(_currentDispatcher);
            mwvm.RecordedItems.Clear();

            (mwvm.provider as XMLStatsProvider).XMLData = XDocument.Load(vm.SelectedSession.XmlDataFile);
            (mwvm.provider as XMLStatsProvider).FileName = vm.SelectedSession.XmlDataFile;

            //Push notifications of records to UI
            (mwvm.provider as XMLStatsProvider).XMLData.Changed += mwvm.XMLData_Changed;

            mwvm.ReloadLoadUIData();

            mwvm.IsSessionLoaded = true;
            mwvm.IsContinuedSession = true;

        }
    }
}
